/*image_widget*/
openerp.image_widget = function(instance) {
	var _t = instance.web._t, _lt = instance.web._lt;
	var QWeb = instance.web.qweb;
    instance.image_widget = {};

    instance.image_widget.crm_image_widget = instance.web.form.AbstractField.extend({
		
		init : function() {
			this._super.apply(this, arguments);
			this.set("value", "");
		},
		start : function() {
			this.display_field();
			this.render_value();
			return this._super();
		},
		display_field : function() {
			this.$el.html(QWeb.render("crm_img", {
				widget : this
			}));
		},
		render_value : function() {
			var html = '<ul class="crm_imgs">';
			var imgVal = this.get("value");
			if ( !imgVal ){
				this.$("#crm_images").html('');
				return false;
			}
			var imgs = imgVal.split(",");
			for (var i = 0; i<imgs.length; i++){
				html += '<li><img src="'+ imgs[i] +'" width="150"/></li>';
			}
			this.$("#crm_images").html(html+'</ul>');
			this.$("#crm_images img").click(function(){
				var imgUrl = $(this).attr("src");
				var w = $(window).width();
				if (  $("#crm_img_dialog").length > 0 ){
					$("#img_show").attr("src" , imgUrl);
				}
				else{
					$("body").append('<div id="crm_img_dialog"><span id="crm_img_close">关闭</span><div style="clear:both;"></div><img id="img_show" src="'+ imgUrl +'" /></div>');
					
					$("#crm_img_close").click(function(){
						$("#crm_img_dialog").remove();
					})
				}
				var img_w = $("#crm_img_dialog").width();
				$("#crm_img_dialog").css({
					"left":(w-img_w)/2+"px"
					})
			})
		}
	});
	

	instance.web.form.widgets.add('crmimage', 'instance.image_widget.crm_image_widget');
}
